/**
 * Created by perqin on 16-12-4.
 */

var passport = require('passport'),
  MultiStrategy = require('./passport-multi/index').Strategy,
  User = require('mongoose').model('User');

module.exports = function (config) {
  // Use multi strategy
  passport.use(new MultiStrategy({
    bits: 6
  }, function (phone, code, done) {
    // function sendCode
    // Send specific <code>code</code> to specific <code>phone</code>
    // Must call callback after sending
    // TODO: Send SMS code
    // Send to console :)
    console.info('SMS code [' + code + '] sent to [' + phone + ']');
    done(null);
  }, function (shotsId, done) {
    // Success callback
    User.findOne({ shots_id: shotsId }, function (err, user) {
      if (err) return done(err);
      if (!user) {
        // Create user
        user = new User();
        user.username = shotsId;
        user.displayName = shotsId;
        user.provider = 'multi';
        user.shots_id = shotsId;
        user.email = shotsId + '@shotsapp.cn';
        console.log(user);
        return user.save(function (err) {
          if (err) return done(err);
          done(null, user);
        });
      }
      done(null, user);
    });
  }));
};
